package com.tgy.algorithm.base;

public class _查找当前元素最右的元素_01 {


    // i 属于 （target <= nums[i]），求i的最小值
    public static int findElementRight(int[] nums, int target) {

        if (nums ==  null || nums.length == 0) {
            return -1;
        }

        int resIndex = -1;

        int left = 0;
        int right = nums.length - 1;

        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            int midVal = nums[mid];
            if(midVal > target) {
                resIndex = mid;
                right = mid - 1;
            }else if(midVal == target) {
                resIndex = mid;
                left = mid + 1;
            }else {
                left = mid + 1;
            }
        }

        return resIndex;
    }

    public static void main(String[] args) {
        int[] nums = {2,2,3,3,5,5,6};
        int elementRight = findElementRight(nums,8);
        System.out.println(elementRight);
    }
}
